home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / useragent.py < prev    next >
Text File  |  2007-06-24  |  5KB  |  159 lines

  1. # -*- test-case-name: nevow.test.test_useragent -*-
  2.  
  3. """
  4. Parsers for browser User-Agent strings.
  5.  
  6. http://en.wikipedia.org/wiki/User_agent
  7. http://www.user-agents.org/
  8. """
  9.  
  10. # Internet Explorer 1.0
  11. # Microsoft Internet Explorer/Version (Platform)
  12.  
  13. # Internet Explorer, and browsers cloaking as Internet Explorer
  14. # Mozilla/MozVer (compatible; MSIE IEVer[; Provider]; Platform[; Extension]*) [Addition]
  15. # MozVer
  16.  
  17. # Netscape < 6.0
  18. # Mozilla/Version[Gold] [[Language]][Provider] (Platform; Security[; SubPlatform][StandAlone])
  19.  
  20. # Mozilla
  21. # Mozilla/MozVer (Platform; Security; SubPlatform; Language; rv:Revision[; Extension]*) Gecko/GeckVer [Product/ProdVer]
  22.  
  23. # Opera
  24. # Opera/Version (Platform; U) [Language]
  25.  
  26.  
  27. class browsers(object):
  28.     """
  29.     Namespace class for Browser identifiers.
  30.     """
  31.     GECKO = u'gecko'
  32.     INTERNET_EXPLORER = u'internet explorer'
  33.     WEBKIT = u'webkit'
  34.     OPERA = u'opera'
  35.  
  36.  
  37. class UserAgent(object):
  38.     """
  39.     Structured representation of a version identifier of a web browser.
  40.  
  41.     This presents only minimal structured information about the agent
  42.     currently.  It could be expanded to include much more information, such a
  43.     security properties, platform, and native language.
  44.  
  45.     @type browser: C{unicode}
  46.     @ivar browser: The broad category of the browser.  Can only take on values
  47.         from L{browsers}.
  48.  
  49.     @type version: C{str}
  50.     @ivar version: The version claimed by the browser.
  51.     """
  52.     def __init__(self, browser, version):
  53.         """
  54.         Initialize a new UserAgent.
  55.  
  56.         The positions of the arguments to this initializer are not stable.
  57.         Only pass arguments by keyword.
  58.         """
  59.         self.browser = browser
  60.         self.version = version
  61.  
  62.  
  63.     def parse_GECKO(cls, agentString):
  64.         """
  65.         Attempt to parse the given User-Agent string as a Gecko-based browser's
  66.         user-agent.
  67.         """
  68.         identifier = 'Gecko/'
  69.         start = agentString.find(identifier)
  70.         if start != -1:
  71.             end = agentString.find(' ', start)
  72.             if end == -1:
  73.                 end = None
  74.             version = agentString[start + len(identifier):end]
  75.             try:
  76.                 version = int(version)
  77.             except ValueError:
  78.                 pass
  79.             else:
  80.                 return cls(browsers.GECKO, (version,))
  81.     parse_GECKO = classmethod(parse_GECKO)
  82.  
  83.  
  84.     def parse_WEBKIT(cls, agentString):
  85.         """
  86.         Attempt to parse the given User-Agent string as a WebKit-based
  87.         browser's user-agent.
  88.         """
  89.         identifier = 'WebKit/'
  90.         start = agentString.find(identifier)
  91.         if start != -1:
  92.             end = start + len(identifier)
  93.             while (
  94.                 end < len(agentString) and
  95.                 agentString[end].isdigit() or
  96.                 agentString[end] == '.'):
  97.                 end += 1
  98.             version = agentString[start + len(identifier):end]
  99.             try:
  100.                 version = map(int, version.split('.'))
  101.             except ValueError:
  102.                 pass
  103.             else:
  104.                 return cls(browsers.WEBKIT, tuple(version))
  105.     parse_WEBKIT = classmethod(parse_WEBKIT)
  106.  
  107.  
  108.     def parse_OPERA(cls, agentString):
  109.         """
  110.         Attempt to parse an Opera user-agent.
  111.         """
  112.         prefix = 'Opera/'
  113.         if agentString.startswith(prefix):
  114.             version = agentString[len(prefix):].split(None, 1)[0]
  115.             try:
  116.                 version = map(int, version.split('.'))
  117.             except ValueError:
  118.                 pass
  119.             else:
  120.                 return cls(browsers.OPERA, tuple(version))
  121.     parse_OPERA = classmethod(parse_OPERA)
  122.  
  123.  
  124.     def parse_MSIE(cls, agentString):
  125.         """
  126.         Attempt to parse an Internet Explorer user-agent.
  127.         """
  128.         oldPrefix = 'Mozilla/4.0 (compatible; MSIE '
  129.         newPrefix = 'Mozilla/5.0 (compatible; MSIE '
  130.         for prefix in oldPrefix, newPrefix:
  131.             if agentString.startswith(prefix):
  132.                 end = agentString.find(';', len(prefix))
  133.                 if end == -1:
  134.                     end = None
  135.                 version = agentString[len(prefix):end]
  136.                 try:
  137.                     version = map(int, version.split('.'))
  138.                 except ValueError:
  139.                     pass
  140.                 else:
  141.                     return cls(browsers.INTERNET_EXPLORER, tuple(version))
  142.     parse_MSIE = classmethod(parse_MSIE)
  143.  
  144.  
  145.     def fromHeaderValue(cls, agentString):
  146.         """
  147.         Attempt to parse an arbitrary user-agent.
  148.  
  149.         @rtype: C{cls} or C{NoneType}
  150.         @return: A user agent object, or C{None} if parsing fails.
  151.         """
  152.         # Order matters here - MSIE parser will match a ton of browsers.
  153.         for parser in ['GECKO', 'WEBKIT', 'MSIE', 'OPERA']:
  154.             agent = getattr(cls, 'parse_' + parser)(agentString)
  155.             if agent is not None:
  156.                 return agent
  157.         return None
  158.     fromHeaderValue = classmethod(fromHeaderValue)
  159.